<?php
/***************************************************************
 *  Copyright notice
 *
 *  (c) 1999-2008 Kasper Skaarhoj (kasperYYYY@typo3.com)
 *  All rights reserved
 *
 *  This script is part of the TYPO3 project. The TYPO3 project is
 *  free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  The GNU General Public License can be found at
 *  http://www.gnu.org/copyleft/gpl.html.
 *  A copy is found in the textfile GPL.txt and important notices to the license
 *  from the author is found in LICENSE.txt distributed with these scripts.
 *
 *
 *  This script is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  This copyright notice MUST APPEAR in all copies of the script!
 ***************************************************************/
/**
 * extending class to class t3lib_basicFileFunctions
 *
 * $Id: class.t3lib_extfilefunc.php 3793 2008-06-11 06:30:14Z ingmars $
 * Revised for TYPO3 3.6 May/2004 by Kasper Skaarhoj
 *
 * @author
 */


unset($MCONF);
require_once('conf.php');
require_once($BACK_PATH.'init.php');
require_once($BACK_PATH.'template.php');
require_once(PATH_t3lib.'class.t3lib_extfilefunc.php');



$LANG->includeLLFile('EXT:user_farm_import/mod1/locallang.xml');

 

/*BRANCH LIST*/
if (!empty($_REQUEST['vC3']))
{
	if(t3lib_div::_GP('vC3') == $GLOBALS['BE_USER']->veriCode())
	{
		if ((0==$_FILES['upload_1']['error'])&&(is_uploaded_file($_FILES['upload_1']['tmp_name']))) {
			$handle = fopen($_FILES['upload_1']['tmp_name'], "r");
			$row=0;
			while (($data = fgetcsv($handle, 1000, ";")) !== FALSE) {
				$row++;
				if ( count($data) != 2){
					echo  sprintf($LANG->getLL('category_csv_format'), count($data), count($data) );
									foreach($data as $val)
					{
					echo $val.'; ';
					}
					fclose($handle);
					EXIT;
				}
			}
			fclose($handle);
		}
		else {
			echo $LANG->getLL('bad_upl_file');
		 exit;}

		 $fileProcessor = t3lib_div::makeInstance('t3lib_extFileFunctions');
		 $fileProcessor->init($FILEMOUNTS, $TYPO3_CONF_VARS['BE']['fileExtensions']);
		 $fileProcessor->init_actionPerms($BE_USER->user['fileoper_perms']);

		 $file = array();
		 $file['rename'][1]['target']= dirname($GLOBALS["temp_path"]).'/fileadmin/import/categories/current_categories.csv';
		 $file['rename'][1]['data']= 'prev_categories.csv';

		 $fileProcessor->start($file);
		 $fileProcessor->processData();
		 unset($file);

		 $file =  t3lib_div::_GP('file');
		 $file['upload'][1]['target']=dirname($GLOBALS["temp_path"]).'/fileadmin/import/categories/';

		 $fileProcessor->start($file);
		 $fileProcessor->processData();
		 unset($file);
		 
		
		 $file = array();
		 $file['rename'][1]['target']= dirname($GLOBALS["temp_path"]).'/fileadmin/import/categories/'.$_FILES['upload_1']['name'];
		 $file['rename'][1]['data']= 'current_categories.csv';
 
		 $fileProcessor->start($file);
		 $fileProcessor->processData();
		 
		 echo $LANG->getLL('ok_upl_file');
		
	}
	else echo $LANG->getLL('wrong_code');
	/**/
}
/*PRODUCTS LIST*/
elseif (!empty($_REQUEST['vC2']))
{
	if(t3lib_div::_GP('vC2') == $GLOBALS['BE_USER']->veriCode())
	{
		if ((0==$_FILES['upload_1']['error'])&&(is_uploaded_file($_FILES['upload_1']['tmp_name']))) {
			$handle = fopen($_FILES['upload_1']['tmp_name'], "r");
			$row=0;
		if ((bool)$handle )	while (($data = fgetcsv($handle, 1000, ";")) !== FALSE) {
				$row++;
				if ( count($data) != 10){
					echo  sprintf($LANG->getLL('product_csv_format'), count($data), count($data) );
					 
					foreach($data as $val)
					{
					echo $val.'; ';
					}
					 
					fclose($handle);
					 EXIT;
				}
			}
			
			else echo $LANG->getLL('bad_upl_file');
			fclose($handle);
		}
		 
	else {
			echo $LANG->getLL('bad_upl_file');
		 exit;}

		 $fileProcessor = t3lib_div::makeInstance('t3lib_extFileFunctions');
		 $fileProcessor->init($FILEMOUNTS, $TYPO3_CONF_VARS['BE']['fileExtensions']);
		 $fileProcessor->init_actionPerms($BE_USER->user['fileoper_perms']);

		 $file = array();
		
		 $file =  t3lib_div::_GP('file');
		 $file['upload'][1]['target']=dirname($GLOBALS["temp_path"]).'/fileadmin/import/products/';

		 $fileProcessor->start($file);
		 $fileProcessor->processData();
		 unset($file);
		 
		 $file['rename'][1]['target']= dirname($GLOBALS["temp_path"]).'/fileadmin/import/products/current_products.csv';
		 $pfile['rename'][1]['data']= 'prev_products.csv';

		 $fileProcessor->start($file);
		 $fileProcessor->processData();
		 unset($file);
		 
		 
		 $file = array();
		 $file['rename'][1]['target']= dirname($GLOBALS["temp_path"]).'/fileadmin/import/products/'.$_FILES['upload_1']['name'];
		 $file['rename'][1]['data']= 'current_products.csv';

		 $fileProcessor->start($file);
		 $fileProcessor->processData();
		 
		echo $LANG->getLL('ok_upl_file');
	}
	else echo $LANG->getLL('wrong_code');

}
/* BRANCH PRICE*/
elseif (!empty($_REQUEST['vC1']))
{
	if(t3lib_div::_GP('vC1') == $GLOBALS['BE_USER']->veriCode())
	{
		if ((0==$_FILES['upload_1']['error'])&&(is_uploaded_file($_FILES['upload_1']['tmp_name']))) {
			$handle = fopen($_FILES['upload_1']['tmp_name'], "r");
			$row=0;
		if ((bool)$handle )	while (($data = fgetcsv($handle, 1000, ";")) !== FALSE) {
				$row++;
				if ( count($data) != 4){
					echo '';
					foreach($data as $val)
					{
					echo $val.'; ';
					}
					 
					fclose($handle);
					 EXIT;
				}
			}
			
			else echo  sprintf($LANG->getLL('branch_csv_format'), count($data), count($data) );
			 
			fclose($handle);
		}
		 
	else {
			echo $LANG->getLL('bad_upl_file');
		 exit;}
		 
		 $branch_id = t3lib_div::_GP('branch_id');

		 $fileProcessor = t3lib_div::makeInstance('t3lib_extFileFunctions');
		 $fileProcessor->init($FILEMOUNTS, $TYPO3_CONF_VARS['BE']['fileExtensions']);
		 $fileProcessor->init_actionPerms($BE_USER->user['fileoper_perms']);

		 $file = array();

		 $file =  t3lib_div::_GP('file');
		 $file['upload'][1]['target']=dirname($GLOBALS["temp_path"]).'/fileadmin/import/prices/branch_'.$branch_id.'/';

		 $fileProcessor->start($file);
		 $fileProcessor->processData();
		 unset($file);
		 
		 $file = array();
		 $file['rename'][1]['target']= dirname($GLOBALS["temp_path"]).'/fileadmin/import/prices/branch_'.$branch_id.'/'.$_FILES['upload_1']['name'];
		 $file['rename'][1]['data']= date('d_m_Y').'.csv';

		 $fileProcessor->start($file);
		 $fileProcessor->processData();
		 
		echo $LANG->getLL('ok_upl_file');
	}
	else echo $LANG->getLL('wrong_code');
	
}




// Include extension?
if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['ext/user_farm_import/mod1/csv_tce_file.php'])	{
	include_once($TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['ext/user_farm_import/mod1/csv_tce_file.php']);
}
?>
